//    Copyright (C) Kherty.  All rights reserved.

using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Threading;
using OpenLS.Spreadsheet.Expressions;

namespace OpenLS.Spreadsheet.UI
{
    partial class DefinedNamesResources
    {

        public DefinedNamesResources()
        {
            InitializeComponent();
        }

        private void OnDefinedNameChanged(object sender, SelectionChangedEventArgs args)
        {
            var cb = (ComboBox) sender;

            var definedName = (DefinedName) cb.SelectedItem;
            var worksheet = (Worksheet) cb.DataContext;
            if (definedName != null)
            {
                object value = definedName.Value;
                if (value is Cell)
                    value = ((Cell) value).ToRange();
                if (value is Range)
                {
                    var range = (Range) value;
                    if (range.Worksheet != worksheet)
                    {
                        worksheet = range.Worksheet;
                        worksheet.Workbook.View.ActiveSheet = worksheet;
                    }
                    worksheet.Selection = range;
                    worksheet.ActiveCell = worksheet.Selection.FirstCell;
                    worksheet.ActivePane.EnsureVisible(worksheet.Selection);
                }
            }

            ReAttachBinding(cb);
        }

        private static void ReAttachBinding(ComboBox cb)
        {
            var thread = new Thread(new ThreadStart(delegate
                                                        {
                                                            Thread.Sleep(100);
                                                            cb.Dispatcher.Invoke(DispatcherPriority.Background,
                                                                                 new MyDelegate(delegate
                                                                                                    {
                                                                                                        var binding =
                                                                                                            new Binding(
                                                                                                                "Selection.NameOrLabel")
                                                                                                                {
                                                                                                                    Mode
                                                                                                                        =
                                                                                                                        BindingMode
                                                                                                                        .
                                                                                                                        OneWay
                                                                                                                };
                                                                                                        cb.SetBinding(
                                                                                                            ComboBox.
                                                                                                                TextProperty,
                                                                                                            binding);
                                                                                                    }));
                                                        }));
            thread.Start(); //\\hmm
        }

        private void OnKeyDown(object sender, KeyEventArgs args)
        {
            if (args.Key == Key.Return)
            {
                var cb = (ComboBox) sender;
                var worksheet = (Worksheet) cb.DataContext;
                string label = cb.Text;
                try
                {
                    if (worksheet.Workbook.IsValidName(label, worksheet))
                    {
                        DefinedName definedName2 = worksheet.Workbook.DefineName(label, worksheet.Selection);
                        // worksheet.Workbook.AddDefinedName(definedName2);
                        Debug.WriteLine("Requery suggested");
                        CommandManager.InvalidateRequerySuggested(); //\\ makes no difference :(
                    }
                    else
                    {
                        CellReference cr = worksheet.ParseCellReference(label);
                        if (cr != null)
                        {
                            worksheet.ActiveCell = cr.Cell;
                            worksheet.Selection = worksheet.ActiveCell.ToRange();
                        }
                    }
                    args.Handled = true;
                    ReAttachBinding(cb);
                }
                catch (ArgumentException)
                {
                }
            }
        }

        #region Nested type: MyDelegate

        private delegate void MyDelegate();

        #endregion
    }
}